package org.mro.grib.util;

import java.lang.reflect.Method;

import org.mro.grib.core.DataRepresentationException;
import org.mro.grib.core.GridException;
import org.mro.grib.core.ProductException;
import org.mro.grib.model.files.secondEdition.dataRepresentations.DataRepresentationTemplateNumber;
import org.mro.grib.model.files.secondEdition.dataRepresentations.DataRepresentationsFactory;
import org.mro.grib.model.files.secondEdition.dataRepresentations.IDataRepresentation;
import org.mro.grib.model.files.secondEdition.grids.GridDefinitionTemplateNumber;
import org.mro.grib.model.files.secondEdition.grids.GridsFactory;
import org.mro.grib.model.files.secondEdition.grids.IGridDefinition;
import org.mro.grib.model.files.secondEdition.products.IProductDefinition;
import org.mro.grib.model.files.secondEdition.products.ProductDefinitionTemplateNumber;
import org.mro.grib.model.files.secondEdition.products.ProductsFactory;

/**
 * Factory class for : - Grids Templates - Product Definition Templates - Data
 * Representation Template
 * 
 * @author MROUANET
 * 
 */
public class GribSecondEditionTemplatesFactory {

	/**
	 * Instanciate an IGridDefinition corresponding to the template number given
	 * in parameter
	 * 
	 * @param templateNumber
	 *            the selected template number
	 * 
	 * @return an instance of the corresponding class
	 * 
	 * @throws GridException
	 *             undefined Grid Definition
	 */
	public static IGridDefinition createGribSecondEdition(GridDefinitionTemplateNumber templateNumber) throws GridException {

		IGridDefinition retour = null;

		try {
			Method gridCreate = GridsFactory.eINSTANCE.getClass().getMethod("createGridDefinitionTemplate3" + templateNumber.getValue());
			retour = IGridDefinition.class.cast(gridCreate.invoke(GridsFactory.eINSTANCE));
		} catch (Throwable e) {
			throw new GridException(e.getMessage());
		}

		return retour;
	}

	/**
	 * Instanciate an IProductDefinition corresponding to the template number
	 * given in parameter
	 * 
	 * @param templateNumber
	 *            the selected template number
	 *            
	 * @return an instance of the corresponding class
	 * 
	 * @throws ProductException
	 *             undefined Product Definition
	 */
	public static IProductDefinition createGribSecondEdition(ProductDefinitionTemplateNumber templateNumber) throws ProductException {
		
		IProductDefinition retour = null;

		try {
			Method productCreate = ProductsFactory.eINSTANCE.getClass().getMethod("createProductDefinitionTemplate4" + templateNumber.getValue());
			retour = IProductDefinition.class.cast(productCreate.invoke(ProductsFactory.eINSTANCE));
		} catch (Throwable e) {
			throw new ProductException(e.getMessage());
		}

		return retour;
	}

	/**
	 * Instanciate an IDataRepresentation corresponding to the template number
	 * given in parameter
	 * 
	 * @param templateNumber the selected template number
	 * 
	 * @return an instance of the corresponding class
	 * 
	 * @throws DataRepresentationException undefined Data Representation
	 */
	public static IDataRepresentation createGribSecondEdition(DataRepresentationTemplateNumber templateNumber) throws DataRepresentationException {
		
		IDataRepresentation retour = null;

		try {
			Method dataRepresentationCreate = DataRepresentationsFactory.eINSTANCE.getClass().getMethod("createDataRepresentationTemplate5" + templateNumber.getValue());
			retour = IDataRepresentation.class.cast(dataRepresentationCreate.invoke(DataRepresentationsFactory.eINSTANCE));
		} catch (Throwable e) {
			throw new DataRepresentationException(e.getMessage());
		}

		return retour;
	}

}
